home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Dev / misc / LOCCounter.lha / LOCCounter / src / Main.cpp < prev    next >
C/C++ Source or Header  |  2004-08-18  |  3KB  |  129 lines

  1. /****************************************************************************
  2. *
  3. * $RCSfile: Main.cpp $
  4. * $Revision: 2.15 $
  5. * $Date: 2004/08/18 21:48:32 $
  6. * $Author: ssolie $
  7. *
  8. *****************************************************************************
  9. *
  10. * Copyright (c) 2004 Steven Solie.  All Rights Reserved.
  11. *
  12. *****************************************************************************
  13. *
  14. * Main.cpp -- C++ LOC Counter
  15. *
  16. * This program counts the lines of code (LOC) in C++ (or C) source code.
  17. * The parser is very simple and relies on the "C++ Coding Standard" and
  18. * the "C++ LOC Standard" to produce a logical LOC count.
  19. */
  20. #include "Main.h"
  21.  
  22. #include "ArgParser.h"
  23. #include "PatternMatch.h"
  24. #include "SourceCode.h"
  25. #include "Tally.h"
  26. #include "FileCount.h"
  27.  
  28. #include <proto/dos.h>
  29.  
  30. #include <ios>
  31. #include <list>
  32. #include <boost/scoped_ptr.hpp>
  33. #include <boost/shared_ptr.hpp>
  34.  
  35.  
  36. namespace {
  37.  
  38. const char* VERSTAG        = "$VER: loc 3.0 (18.8.04)";
  39. const char* APPNAME        = "loc";
  40. const char* TEMPLATE    = "FILE/M/A,TOTAL/S,DIFF=DIFFERENCE/S,QUICK/S";
  41. const char* ARG_FILE    = "FILE";
  42. const char* ARG_TOTAL    = "TOTAL";
  43. const char* ARG_DIFF    = "DIFF";
  44. const char* ARG_QUICK    = "QUICK";
  45.  
  46. };
  47.  
  48.  
  49. /*
  50.  * main -- Main entry point
  51.  *
  52.  * This is the main entry point for the program.  Returns a valid DOS
  53.  * return code indicating success or failure.
  54.  */
  55. int main()
  56. {
  57.     extern struct WBStartup* __WBenchMsg;
  58.     if ( __WBenchMsg != 0 )  {
  59.         return RETURN_FAIL;
  60.     }
  61.  
  62.     std::ios::sync_with_stdio(false);
  63.  
  64.     ArgParser parser;
  65.     if ( parser.parse(TEMPLATE) == false )  {
  66.         IDOS->PrintFault(IDOS->IoErr(), const_cast<STRPTR>(APPNAME));
  67.         return RETURN_FAIL;
  68.     }
  69.  
  70.     const bool show_diff = parser.getBoolean(ARG_DIFF, false);
  71.     const bool show_total = parser.getBoolean(ARG_TOTAL, false);
  72.     const bool quick = parser.getBoolean(ARG_QUICK, false);
  73.     const BPTR out = IDOS->Output();
  74.  
  75.     // Count the LOC and tally the results.
  76.     int32 result = RETURN_OK;
  77.  
  78.     try  {
  79.         PatternMatch match;
  80.         std::list< boost::shared_ptr<FileCount> > file_counts;
  81.  
  82.         Tally tally(show_diff);
  83.  
  84.         char* pattern = parser.firstString(ARG_FILE);
  85.         while ( pattern != 0 )  {
  86.             const char* file_name = match.firstFile(pattern);
  87.             while ( file_name != 0 )  {
  88.                 boost::shared_ptr<FileCount> file_count(new FileCount(file_name, show_diff));
  89.                 file_counts.push_back(file_count);
  90.  
  91.                 boost::scoped_ptr<SourceCode> source(new SourceCode(*file_count));
  92.                 source->load();
  93.                 source->count();
  94.  
  95.                 tally.addFile(*file_count);
  96.                 file_name = match.nextFile();
  97.             }
  98.  
  99.             pattern = parser.nextString();
  100.         }
  101.  
  102.         if ( quick )  {
  103.             tally.printQuick(out);
  104.         }
  105.         else  {
  106.             if ( show_total )  {
  107.                 tally.printTotal(out);
  108.             }
  109.             else  {
  110.                 tally.print(out);
  111.             }
  112.         }
  113.     }
  114.     catch ( std::bad_alloc )  {
  115.         IDOS->SetIoErr(ERROR_NO_FREE_STORE);
  116.         IDOS->PrintFault(ERROR_NO_FREE_STORE, const_cast<STRPTR>(APPNAME));
  117.         result = RETURN_FAIL;
  118.     }
  119.     catch ( dos_error )  {
  120.         IDOS->PrintFault(IDOS->IoErr(), const_cast<STRPTR>(APPNAME));
  121.         result = RETURN_ERROR;
  122.     }
  123.     catch ( ... )  {
  124.         result = RETURN_FAIL;
  125.     }
  126.  
  127.     return result;
  128. }
  129.